import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_page/mine_business/mine_setting/setting_avatar_logic.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/global_store/store.dart';
import 'package:tiktokadult/tools_base/loading/l_error_widget.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';
import 'package:tiktokadult/tools_base/widget/net_image_widget.dart';

class SettingAvatarPage extends StatelessWidget {
  static const String routeName = '/setting/avatar';
  const SettingAvatarPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: SettingAvatarLogic(),
      builder: (controller) => Scaffold(
        appBar: AppBar(
          title: Text('选择头像'),
          actions: [
            InkWell(
              onTap: controller.confirmChangeAvatar,
              child: Text(
                '保存',
                style: TextStyle(color: Colors.white.withOpacity(0.55), fontSize: 12),
              ),
            ),
            16.sizeBoxW,
          ],
        ),
        body: () {
          if (controller.isLoading) return LoadingCenterWidget();
          if (controller.dataSource.isEmpty) return CErrorWidget();
          return Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              30.verticalSpace,
              NetworkImageLoader(
                imageUrl: context.watch<GlobalStore>().meInfo?.portrait ?? '',
                width: 90,
                height: 90,
                borderRadius: 45,
              ),
              10.verticalSpace,
              Text(
                '当前头像',
                style: TextStyle(color: Colors.white.withOpacity(0.55), fontSize: 12),
              ),
              20.verticalSpace,
              Expanded(
                child: GridView.builder(
                  padding: EdgeInsets.only(left: 38, right: 38, top: 0),
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: controller.crossCount,
                    mainAxisSpacing: controller.spaceV,
                    crossAxisSpacing: controller.spaceH,
                    childAspectRatio: controller.ratio,
                  ),
                  itemCount: controller.dataSource.length,
                  itemBuilder: (BuildContext context, int index) {
                    final select = controller.selectIndex == index;
                    return GestureDetector(
                      onTap: () => controller.selectAavatar(index),
                      child: Stack(
                        children: [
                          NetworkImageLoader(
                            imageUrl: controller.dataSource[index],
                            width: double.infinity,
                            height: double.infinity,
                            borderRadius: 100,
                          ),
                          if (select)
                            Align(
                              alignment: Alignment.bottomRight,
                              child: Image.asset(
                                'red_checked.png'.mineImgPath,
                                width: 22,
                                height: 22,
                              ),
                            )
                        ],
                      ),
                    );
                  },
                ),
              ),
              12.sizeBoxH,
            ],
          );
        }(),
      ),
    );
  }
}
